package algorithm.middle;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个函数  f(x, y) 和一个目标结果 z，函数公式未知，请你计算方程 f(x,y) == z 所有可能的正整数 数对 x 和 y。满足条件的结果数对可以按任意顺序返回。
 */
public class FindSolution1237 {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> res = new ArrayList<>();
        //双指针，x+，y-
        int y = 1000;
        for (int x = 1; x <= 1000 && y>0; x++) {
            ArrayList<Integer> temp = new ArrayList<>();
            while (y > 0 && customfunction.f(x, y) > z) {
                y--;
            }
            if (customfunction.f(x, y) == z && y!=0) {
                temp.add(x);
                temp.add(y);
                res.add(temp);
            }
        }
        return res;
    }

    abstract class CustomFunction {
        // Returns f(x, y) for any given positive integers x and y.
        // Note that f(x, y) is increasing with respect to both x and y.
        // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
        public abstract int f(int x, int y);
    }


}
